class ProductcommentsController < ApplicationController
  layout"background"
  before_filter:authorize,
                 :only=>[:new,:create]
  
  def authorize
    unless User.find_by_id(session[:user_id])
      notice("请先登录！")
      session[:last_uri]=request.request_uri
      redirect_to_login
    end
  end
  
  def index
    @productcomments = Productcomment.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @productcomments }
    end
  end

  # GET /productcomments/1
  # GET /productcomments/1.xml
  def show
    @productcomment = Productcomment.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @productcomment }
    end
  end

  # GET /productcomments/new
  # GET /productcomments/new.xml
  def new
    @productcomment = Productcomment.new
    @product=Product.find(params[:product_id])
    @order=Order.find(params[:order_id])

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @productcomment }
    end
  end

  # GET /productcomments/1/edit
  def edit
    @productcomment = Productcomment.find(params[:id])
  end

  # POST /productcomments
  # POST /productcomments.xml
  def create
    @productcomment = Productcomment.new(params[:productcomment])
    @productcomment.product_id=params[:product_id]
    @productcomment.user_id=session[:user_id]

      if @productcomment.save
        notice("评论成功！")
      else
        notice("评论失败！")
      end
      redirect_to(:controller=>:products,:action=>:show,:id=>params[:product_id])
    end

  # PUT /productcomments/1
  # PUT /productcomments/1.xml
  def update
    @productcomment = Productcomment.find(params[:id])

    respond_to do |format|
      if @productcomment.update_attributes(params[:productcomment])
        flash[:notice] = 'Productcomment was successfully updated.'
        format.html { redirect_to(@productcomment) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @productcomment.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /productcomments/1
  # DELETE /productcomments/1.xml
  def destroy
    @productcomment = Productcomment.find(params[:id])
    @productcomment.destroy

    respond_to do |format|
      format.html { redirect_to(productcomments_url) }
      format.xml  { head :ok }
    end
  end
end
